<?php

namespace App\Http\Controllers\Admin;

use App\Http\Helpers\Sign;
use App\Http\Services\AdminService;
use App\Http\Services\MsgService;
use App\Http\Services\QuestionService;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Http\Services\RuleService;
use Maatwebsite\Excel\Facades\Excel;
use App\Http\Helpers\Crypter;

class IndexController extends Controller
{
    /**
     *首页内页
     * @author: lican<364548233@qq.com>
     */
    public function index_v1(Request $request){
//        return view('admin.index.index_v1');
    }

    /**
     *获取PC端aiSee的url
     * @author: lican<364548233@qq.com>
     */
    public function getPcAiseeUrl(Request $request){
        $this->validate($request, [
            'admin' => 'required'
        ]);
        $user = $request->input('admin');
        $userId = $user['name']."(".$user['card_number'].")"."[".$user['school']['name']."]"."|".$user['school']['code'];

        $crypter = new Crypter();
        $data = [
            'userid' => $userId,
            'ip' => Sign::getClientIp(),
//            'hardware' => Sign::getPhoneModel(),
            'os' => php_uname('s').php_uname('r'),
            't' => time()
        ];
        $decrypt = [
            'color' => "08c27c"
        ];
        $url = $crypter::getAuthorizedUrl(BASE_PC_SUBMIT, $data, $decrypt);

        header("location:{$url}");

        return MsgService::getMsg(0, ['url' => $url]);
    }

    /**
     *获取管理员待办
     * @author: lican<364548233@qq.com>
     */
    public function todo(Request $request){
        $this->validate($request, [
            'admin' => 'required'
        ]);
        $admin = $request->input('admin');
        $res = QuestionService::getAdminTodo($admin['id']);
        return $res;
    }

    /**
     *后台首页
     * @author: lican<364548233@qq.com>
     */
    public function index(Request $request){
        $this->validate($request, [
            'admin' => 'required'
        ]);
        $admin = $request->input('admin');
        $res = AdminService::getAdminDeatil($admin['id']);
        return $res;
    }

    /**
     *获取菜单栏
     * @author: lican<364548233@qq.com>
     */
    public function getMenu(Request $request){
        $this->validate($request, [
            'admin' => 'required'
        ]);
        $adminId = ($request->input('admin'))['id'];
        $rule = RuleService::getMenu($adminId);
        return $rule;
    }

    /**
     *读excel
     * @author: lican<364548233@qq.com>
     */
    public function read(Request $request){
        $filePath = './order.xlsx';
        $data = Excel::load($filePath, function($reader) {
            $data = $reader->all();
        })->toArray();

        $list = [];

        print_r($list);
//        $this->export($list);
    }

    /**
     *导出
     * @author: lican<364548233@qq.com>
     */
    public function export($cellData){

        Excel::create('search',function($excel) use ($cellData){

            $excel->sheet('search', function($sheet) use ($cellData){

                $sheet->rows($cellData);

            });

        })->store('xls')->export('xls');
    }

}
